In [32]:
import geopandas
import numpy as np
import pandas as pd
from shapely.geometry import Point

import missingno as msn

import seaborn as sns
import matplotlib.pyplot as plt

% matplotlib inline
UsageError: Line magic function `%` not found.
In [33]:
new=pd.read_csv("data/Police Facility Locations.csv")
new
Out[33]:
_id PROVIDER ADDRESS FACILITY_NAME POSTAL_CODE LONG LAT
0 1 Toronto Police Services 2054 Davenport Road 11 Division M6N 1C8 -79.460825 43.671071
1 2 Toronto Police Services 200 Trethewey Drive 12 Division M6M 5E6 -79.486873 43.694571
2 3 Toronto Police Services 1435 Eglinton Avenue West 13 Division M6C 3Z4 -79.436680 43.698319
3 4 Toronto Police Services 350 Dovercourt Road 14 Division M6J 3E3 -79.425978 43.651297
4 5 Toronto Police Services 3699 Bloor Street West 22 Division M9A 1A2 -79.529181 43.643098
5 6 Toronto Police Services 5230 Finch Avenue West 23 Division M9V 0A1 -79.583518 43.743857
6 7 Toronto Police Services 40 Norfinch Drive 31 Division M3N 1X1 -79.527471 43.756742
7 8 Toronto Police Services 30 Ellerslie Avenue 32 Division M2N 1X8 -79.415084 43.771720
8 9 Toronto Police Services 50 Upjohn Road 33 Division M3B 2W1 -79.350065 43.751073
9 10 Toronto Police Services 2222 Eglinton Avenue East 41 Division M1K 2M2 -79.277107 43.730797
10 11 Toronto Police Services 242 Milner Avenue 42 Division M1S 5C4 -79.240009 43.789365
11 12 Toronto Police Services 4331 Lawrence Avenue East 43 Division M1E 2T4 -79.174053 43.770827
12 13 Toronto Police Services 51 Parliament Street 51 Division M5A 2Y5 -79.362139 43.651943
13 14 Toronto Police Services 255 Dundas Street West 52 Division M5T 2W5 -79.389714 43.654200
14 15 Toronto Police Services 75 Eglinton Avenue West 53 Division M4R 2G9 -79.400659 43.706051
15 16 Toronto Police Services 41 Cranfield Road 54 Division M4B 3H6 -79.309480 43.714600
16 17 Toronto Police Services 101 Coxwell Avenue 55 Division M4L 3B3 -79.317296 43.669171
17 23 Toronto Police Services 40 College Street Headquarters M5G 2J3 -79.385140 43.661349
In [34]:
import json
def convert(o):
    if isinstance(o, np.int64): return int(o)  
    raise TypeError

json.dumps({'value': np.int64(42)}, default=convert)
Out[34]:
'{"value": 42}'
In [35]:
import folium
import pandas as pd

SF_COORDINATES = (43.7001100,-79.4163000)

state_geo = geopandas.read_file("data/Neighbourhoods.GeoJson")

crimedata=pd.read_csv("data/MCI_2014_to_2019 - Copy.csv")

MAX_RECORDS = 1000
 
mapp = folium.Map(location=SF_COORDINATES, tiles='Stamen Terrain', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "

crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))

crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']

folium.Choropleth(
    geo_data=state_geo,
              #data_out = 'crimeagg.json', 
              data = crimedata2,
              columns = ['Neighbour', 'Number'],
              key_on = 'feature.id',
              fill_color = 'BuPu', 
              fill_opacity = 0, 
              line_opacity = 0.2,
              legend_name = 'Number of incidents per Neibourhood').add_to(mapp)


folium.GeoJson(state_geo, name='geojson').add_to(mapp)

for index, each in new[0:MAX_RECORDS].iterrows():
    
  popup_text = popup_text.format(each["FACILITY_NAME"],
                          each["ADDRESS"])
  folium.Marker(
        location = [each['LAT'],each['LONG']],
        popup=popup_text,icon=folium.Icon(color='red'),
        clustered_marker = True).add_to(mapp)
  folium.Circle(
        location = [each['LAT'],each['LONG']],radius=5000).add_to(mapp)
   
    
 
mapp.save('mappolice.html')
In [36]:
crimedata2
Out[36]:
Neighbour Number
0 Waterfront Communities-The Island (77) 7747
1 Bay Street Corridor (76) 6817
2 Church-Yonge Corridor (75) 6232
3 West Humber-Clairville (1) 5702
4 Moss Park (73) 4786
... ... ...
135 Yonge-St.Clair (97) 412
136 Guildwood (140) 411
137 Maple Leaf (29) 410
138 Woodbine-Lumsden (60) 377
139 Lambton Baby Point (114) 353

140 rows × 2 columns

In [37]:
crimedata.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 206435 entries, 0 to 206434
Data columns (total 25 columns):
 #   Column               Non-Null Count   Dtype  
---  ------               --------------   -----  
 0   X                    206435 non-null  float64
 1   Y                    206435 non-null  float64
 2   Index_               206435 non-null  int64  
 3   event_unique_id      206435 non-null  object 
 4   occurrencedate       206435 non-null  object 
 5   occtime              206435 non-null  object 
 6   occdatetime          206435 non-null  object 
 7   reporteddate         206435 non-null  object 
 8   date                 206435 non-null  int64  
 9   reptime              206435 non-null  object 
 10  repdatetime          206435 non-null  object 
 11  DELAY                206435 non-null  object 
 12  premisetype          206435 non-null  object 
 13  ucr_code             206435 non-null  int64  
 14  ucr_ext              206435 non-null  int64  
 15  offence              206435 non-null  object 
 16  reporteddayofyear    206435 non-null  int64  
 17  occurrencedayofyear  206376 non-null  float64
 18  MCI                  206435 non-null  object 
 19  Division             206435 non-null  object 
 20  Hood_ID              206435 non-null  int64  
 21  Neighbourhood        206435 non-null  object 
 22  Long                 206435 non-null  float64
 23  Lat                  206435 non-null  float64
 24  ObjectId             206435 non-null  int64  
dtypes: float64(5), int64(7), object(13)
memory usage: 39.4+ MB
In [38]:
crime=pd.read_csv("data/MCI_2014_to_2019.csv")
crime.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 206435 entries, 0 to 206434
Data columns (total 29 columns):
 #   Column               Non-Null Count   Dtype  
---  ------               --------------   -----  
 0   X                    206435 non-null  float64
 1   Y                    206435 non-null  float64
 2   Index_               206435 non-null  int64  
 3   event_unique_id      206435 non-null  object 
 4   occurrencedate       206435 non-null  object 
 5   reporteddate         206435 non-null  object 
 6   premisetype          206435 non-null  object 
 7   ucr_code             206435 non-null  int64  
 8   ucr_ext              206435 non-null  int64  
 9   offence              206435 non-null  object 
 10  reportedyear         206435 non-null  int64  
 11  reportedmonth        206435 non-null  object 
 12  reportedday          206435 non-null  int64  
 13  reporteddayofyear    206435 non-null  int64  
 14  reporteddayofweek    206435 non-null  object 
 15  reportedhour         206435 non-null  int64  
 16  occurrenceyear       206376 non-null  float64
 17  occurrencemonth      206376 non-null  object 
 18  occurrenceday        206376 non-null  float64
 19  occurrencedayofyear  206376 non-null  float64
 20  occurrencedayofweek  206376 non-null  object 
 21  occurrencehour       206435 non-null  int64  
 22  MCI                  206435 non-null  object 
 23  Division             206435 non-null  object 
 24  Hood_ID              206435 non-null  int64  
 25  Neighbourhood        206435 non-null  object 
 26  Long                 206435 non-null  float64
 27  Lat                  206435 non-null  float64
 28  ObjectId             206435 non-null  int64  
dtypes: float64(7), int64(10), object(12)
memory usage: 45.7+ MB
In [39]:
new_mci=['Assault','Auto Theft','Break and Enter']
crime=crime[crime.MCI.isin(new_mci)]
In [40]:
crime
Out[40]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... occurrencedayofyear occurrencedayofweek occurrencehour MCI Division Hood_ID Neighbourhood Long Lat ObjectId
0 -79.405228 43.656982 7801 GO-20152165447 2015-12-18T03:58:00.000Z 2015-12-18T03:59:00.000Z Commercial 1430 100 Assault ... 352.0 Friday 3 Assault D14 79 University (79) -79.405228 43.656982 7001
1 -79.307907 43.778732 7802 GO-20151417245 2015-08-15T21:45:00.000Z 2015-08-17T22:11:00.000Z Commercial 1430 100 Assault ... 227.0 Saturday 21 Assault D42 118 Tam O'Shanter-Sullivan (118) -79.307907 43.778732 7002
2 -79.225029 43.765942 7803 GO-20151421107 2015-08-16T16:00:00.000Z 2015-08-18T14:40:00.000Z Apartment 2120 200 B&E ... 228.0 Sunday 16 Break and Enter D43 137 Woburn (137) -79.225029 43.765942 7003
3 -79.140823 43.778648 7804 GO-20152167714 2015-11-26T13:00:00.000Z 2015-12-18T13:38:00.000Z Other 2120 200 B&E ... 330.0 Thursday 13 Break and Enter D43 133 Centennial Scarborough (133) -79.140823 43.778648 7004
4 -79.288361 43.691235 7805 GO-20152169954 2015-12-18T19:50:00.000Z 2015-12-18T19:55:00.000Z Commercial 1430 100 Assault ... 352.0 Friday 19 Assault D55 61 Taylor-Massey (61) -79.288361 43.691235 7005
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
206430 -79.563538 43.682293 205896 GO-2019336030 2019-02-21T23:00:00.000Z 2019-02-22T13:27:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 52.0 Thursday 23 Auto Theft D23 7 Willowridge-Martingrove-Richview (7) -79.563538 43.682293 205996
206431 -79.348709 43.656227 205897 GO-2019336549 2019-02-21T00:01:00.000Z 2019-02-22T14:45:00.000Z Commercial 2135 210 Theft Of Motor Vehicle ... 52.0 Thursday 0 Auto Theft D55 70 South Riverdale (70) -79.348709 43.656227 205997
206432 -79.598869 43.743992 205898 GO-2019187512 2019-01-30T12:30:00.000Z 2019-01-30T12:47:00.000Z House 2135 210 Theft Of Motor Vehicle ... 30.0 Wednesday 12 Auto Theft D23 1 West Humber-Clairville (1) -79.598869 43.743992 205998
206433 -79.348709 43.656227 205899 GO-2019336549 2019-02-21T00:01:00.000Z 2019-02-22T14:45:00.000Z Commercial 2135 210 Theft Of Motor Vehicle ... 52.0 Thursday 0 Auto Theft D55 70 South Riverdale (70) -79.348709 43.656227 205999
206434 -79.217125 43.814865 205900 GO-2019327189 2019-02-21T08:00:00.000Z 2019-02-21T08:09:00.000Z House 2135 210 Theft Of Motor Vehicle ... 52.0 Thursday 8 Auto Theft D42 132 Malvern (132) -79.217125 43.814865 206000

178105 rows × 29 columns

In [41]:
crime.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 178105 entries, 0 to 206434
Data columns (total 29 columns):
 #   Column               Non-Null Count   Dtype  
---  ------               --------------   -----  
 0   X                    178105 non-null  float64
 1   Y                    178105 non-null  float64
 2   Index_               178105 non-null  int64  
 3   event_unique_id      178105 non-null  object 
 4   occurrencedate       178105 non-null  object 
 5   reporteddate         178105 non-null  object 
 6   premisetype          178105 non-null  object 
 7   ucr_code             178105 non-null  int64  
 8   ucr_ext              178105 non-null  int64  
 9   offence              178105 non-null  object 
 10  reportedyear         178105 non-null  int64  
 11  reportedmonth        178105 non-null  object 
 12  reportedday          178105 non-null  int64  
 13  reporteddayofyear    178105 non-null  int64  
 14  reporteddayofweek    178105 non-null  object 
 15  reportedhour         178105 non-null  int64  
 16  occurrenceyear       178051 non-null  float64
 17  occurrencemonth      178051 non-null  object 
 18  occurrenceday        178051 non-null  float64
 19  occurrencedayofyear  178051 non-null  float64
 20  occurrencedayofweek  178051 non-null  object 
 21  occurrencehour       178105 non-null  int64  
 22  MCI                  178105 non-null  object 
 23  Division             178105 non-null  object 
 24  Hood_ID              178105 non-null  int64  
 25  Neighbourhood        178105 non-null  object 
 26  Long                 178105 non-null  float64
 27  Lat                  178105 non-null  float64
 28  ObjectId             178105 non-null  int64  
dtypes: float64(7), int64(10), object(12)
memory usage: 40.8+ MB
In [42]:
crime["datetime"] = pd.to_datetime(crime["occurrencedate"])
crime["datetime2"] = pd.to_datetime(crime["reporteddate"])
crime['year'] = crime['datetime'].dt.year
crime['hour'] = crime['datetime'].dt.hour
crime['Delay']=crime["datetime2"]-crime["datetime"]
In [43]:
crime['Delay']
Out[43]:
0         0 days 00:01:00
1         2 days 00:26:00
2         1 days 22:40:00
3        22 days 00:38:00
4         0 days 00:05:00
               ...       
206430    0 days 14:27:00
206431    1 days 14:44:00
206432    0 days 00:17:00
206433    1 days 14:44:00
206434    0 days 00:09:00
Name: Delay, Length: 178105, dtype: timedelta64[ns]
In [44]:
crime=crime[crime['Delay']<=datetime.timedelta(days=1)]
crime
Out[44]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
0 -79.405228 43.656982 7801 GO-20152165447 2015-12-18T03:58:00.000Z 2015-12-18T03:59:00.000Z Commercial 1430 100 Assault ... 79 University (79) -79.405228 43.656982 7001 2015-12-18 03:58:00+00:00 2015-12-18 03:59:00+00:00 2015 3 00:01:00
4 -79.288361 43.691235 7805 GO-20152169954 2015-12-18T19:50:00.000Z 2015-12-18T19:55:00.000Z Commercial 1430 100 Assault ... 61 Taylor-Massey (61) -79.288361 43.691235 7005 2015-12-18 19:50:00+00:00 2015-12-18 19:55:00+00:00 2015 19 00:05:00
10 -79.611320 43.710689 7811 GO-20151976877 2015-11-18T05:35:00.000Z 2015-11-18T05:42:00.000Z Commercial 1480 100 Assault - Resist/ Prevent Seiz ... 1 West Humber-Clairville (1) -79.611320 43.710690 7011 2015-11-18 05:35:00+00:00 2015-11-18 05:42:00+00:00 2015 5 00:07:00
11 -79.436020 43.698772 7812 GO-20152060024 2015-12-01T15:10:00.000Z 2015-12-01T15:20:00.000Z Other 1430 100 Assault ... 102 Forest Hill North (102) -79.436020 43.698772 7012 2015-12-01 15:10:00+00:00 2015-12-01 15:20:00+00:00 2015 15 00:10:00
12 -79.508636 43.720917 7813 GO-20152061875 2015-12-01T22:19:00.000Z 2015-12-01T22:19:00.000Z Commercial 1430 100 Assault ... 26 Downsview-Roding-CFB (26) -79.508636 43.720917 7013 2015-12-01 22:19:00+00:00 2015-12-01 22:19:00+00:00 2015 22 00:00:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
206428 -79.593422 43.738930 205894 GO-2019333589 2019-02-21T23:45:00.000Z 2019-02-22T06:00:00.000Z House 2135 210 Theft Of Motor Vehicle ... 1 West Humber-Clairville (1) -79.593422 43.738930 205994 2019-02-21 23:45:00+00:00 2019-02-22 06:00:00+00:00 2019 23 06:15:00
206429 -79.416000 43.781639 205895 GO-2019329984 2019-02-21T07:30:00.000Z 2019-02-21T15:19:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 36 Newtonbrook West (36) -79.416000 43.781639 205995 2019-02-21 07:30:00+00:00 2019-02-21 15:19:00+00:00 2019 7 07:49:00
206430 -79.563538 43.682293 205896 GO-2019336030 2019-02-21T23:00:00.000Z 2019-02-22T13:27:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 7 Willowridge-Martingrove-Richview (7) -79.563538 43.682293 205996 2019-02-21 23:00:00+00:00 2019-02-22 13:27:00+00:00 2019 23 14:27:00
206432 -79.598869 43.743992 205898 GO-2019187512 2019-01-30T12:30:00.000Z 2019-01-30T12:47:00.000Z House 2135 210 Theft Of Motor Vehicle ... 1 West Humber-Clairville (1) -79.598869 43.743992 205998 2019-01-30 12:30:00+00:00 2019-01-30 12:47:00+00:00 2019 12 00:17:00
206434 -79.217125 43.814865 205900 GO-2019327189 2019-02-21T08:00:00.000Z 2019-02-21T08:09:00.000Z House 2135 210 Theft Of Motor Vehicle ... 132 Malvern (132) -79.217125 43.814865 206000 2019-02-21 08:00:00+00:00 2019-02-21 08:09:00+00:00 2019 8 00:09:00

145458 rows × 34 columns

In [45]:
crimea2 = pd.DataFrame(crime['Delay'].value_counts().astype(int))

crimea2 = crimea2.reset_index()
crimea2.columns = ['Delay', 'Number']
In [46]:
crimea2
Out[46]:
Delay Number
0 00:00:00 40027
1 00:01:00 3162
2 00:02:00 3069
3 00:03:00 3059
4 00:05:00 2942
... ... ...
1436 20:16:00 5
1437 22:44:00 4
1438 21:37:00 4
1439 22:02:00 4
1440 23:24:00 3

1441 rows × 2 columns

In [47]:
crimea2= crimea2[crimea2['Number']>1]
crimea2= crimea2[crimea2['Delay']<=datetime.timedelta(days=1)]
crimea2= crimea2[crimea2['Delay']>=datetime.timedelta(minutes=1)]
crimea2.head(10)
Out[47]:
Delay Number
1 00:01:00 3162
2 00:02:00 3069
3 00:03:00 3059
4 00:05:00 2942
5 00:04:00 2865
6 00:06:00 2337
7 00:07:00 1845
8 00:08:00 1716
9 00:10:00 1510
10 00:09:00 1495
In [48]:
(crimea2['Number']).sum()
Out[48]:
105431
In [49]:
import datetime
new2014= crime[crime['year']==2014]
new2014= new2014[new2014['Delay']>=datetime.timedelta(minutes=6)]
new2014
Out[49]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
307 -79.425995 43.642769 7082 GO-20143348754 2014-11-21T23:30:00.000Z 2014-11-22T17:36:00.000Z Commercial 1430 100 Assault ... 84 Little Portugal (84) -79.425995 43.642769 7308 2014-11-21 23:30:00+00:00 2014-11-22 17:36:00+00:00 2014 23 18:06:00
466 -79.513802 43.719833 7414 GO-20141821905 2014-04-02T18:00:00.000Z 2014-04-03T16:47:00.000Z Apartment 2120 200 B&E ... 26 Downsview-Roding-CFB (26) -79.513802 43.719833 7467 2014-04-02 18:00:00+00:00 2014-04-03 16:47:00+00:00 2014 18 22:47:00
559 -79.382378 43.671223 7923 GO-20141587326 2014-02-23T13:00:00.000Z 2014-02-24T07:31:00.000Z Other 2120 200 B&E ... 75 Church-Yonge Corridor (75) -79.382378 43.671223 7560 2014-02-23 13:00:00+00:00 2014-02-24 07:31:00+00:00 2014 13 18:31:00
631 -79.304970 43.672829 7965 GO-20143345449 2014-11-21T23:30:00.000Z 2014-11-22T00:05:00.000Z House 1430 100 Assault ... 63 The Beaches (63) -79.304970 43.672829 7632 2014-11-21 23:30:00+00:00 2014-11-22 00:05:00+00:00 2014 23 00:35:00
632 -79.391136 43.649898 7966 GO-20143344734 2014-11-21T11:46:00.000Z 2014-11-22T00:44:00.000Z Outside 1420 100 Assault With Weapon ... 78 Kensington-Chinatown (78) -79.391136 43.649898 7633 2014-11-21 11:46:00+00:00 2014-11-22 00:44:00+00:00 2014 11 12:58:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
70994 -79.268341 43.814259 58495 GO-20141401237 2014-01-24T01:00:00.000Z 2014-01-24T09:40:00.000Z House 2135 210 Theft Of Motor Vehicle ... 129 Agincourt North (129) -79.268341 43.814259 58995 2014-01-24 01:00:00+00:00 2014-01-24 09:40:00+00:00 2014 1 08:40:00
70996 -79.486977 43.736629 58497 GO-20141407635 2014-01-24T23:30:00.000Z 2014-01-25T11:14:00.000Z House 2135 210 Theft Of Motor Vehicle ... 26 Downsview-Roding-CFB (26) -79.486977 43.736629 58997 2014-01-24 23:30:00+00:00 2014-01-25 11:14:00+00:00 2014 23 11:44:00
70997 -79.327560 43.726646 58498 GO-20141407886 2014-01-24T13:00:00.000Z 2014-01-25T12:08:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 42 Banbury-Don Mills (42) -79.327560 43.726646 58998 2014-01-24 13:00:00+00:00 2014-01-25 12:08:00+00:00 2014 13 23:08:00
70998 -79.327560 43.726646 58499 GO-20141407886 2014-01-24T13:00:00.000Z 2014-01-25T12:08:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 42 Banbury-Don Mills (42) -79.327560 43.726646 58999 2014-01-24 13:00:00+00:00 2014-01-25 12:08:00+00:00 2014 13 23:08:00
70999 -79.461227 43.673939 58500 GO-20141404988 2014-01-24T21:15:00.000Z 2014-01-24T21:27:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 91 Weston-Pellam Park (91) -79.461227 43.673939 59000 2014-01-24 21:15:00+00:00 2014-01-24 21:27:00+00:00 2014 21 00:12:00

14706 rows × 34 columns

In [50]:
new2015= crime[crime['year']==2015]
new2015= new2015[new2015['Delay']>=datetime.timedelta(minutes=6)]
new2015
Out[50]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
10 -79.611320 43.710689 7811 GO-20151976877 2015-11-18T05:35:00.000Z 2015-11-18T05:42:00.000Z Commercial 1480 100 Assault - Resist/ Prevent Seiz ... 1 West Humber-Clairville (1) -79.611320 43.710690 7011 2015-11-18 05:35:00+00:00 2015-11-18 05:42:00+00:00 2015 5 00:07:00
11 -79.436020 43.698772 7812 GO-20152060024 2015-12-01T15:10:00.000Z 2015-12-01T15:20:00.000Z Other 1430 100 Assault ... 102 Forest Hill North (102) -79.436020 43.698772 7012 2015-12-01 15:10:00+00:00 2015-12-01 15:20:00+00:00 2015 15 00:10:00
14 -79.209801 43.814213 7815 GO-20152101269 2015-12-08T09:15:00.000Z 2015-12-08T09:27:00.000Z Other 1430 100 Assault ... 132 Malvern (132) -79.209801 43.814213 7015 2015-12-08 09:15:00+00:00 2015-12-08 09:27:00+00:00 2015 9 00:12:00
15 -79.359757 43.650761 7816 GO-20152122687 2015-12-11T13:15:00.000Z 2015-12-11T13:45:00.000Z Outside 1430 100 Assault ... 77 Waterfront Communities-The Island (77) -79.359756 43.650761 7016 2015-12-11 13:15:00+00:00 2015-12-11 13:45:00+00:00 2015 13 00:30:00
17 -79.565308 43.737240 7818 GO-20152156790 2015-12-16T17:00:00.000Z 2015-12-16T19:29:00.000Z Outside 1430 100 Assault ... 3 Thistletown-Beaumond Heights (3) -79.565308 43.737240 7018 2015-12-16 17:00:00+00:00 2015-12-16 19:29:00+00:00 2015 17 02:29:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
129338 -79.515686 43.749149 125094 GO-20162866 2015-12-31T21:00:00.000Z 2016-01-01T13:05:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 25 Glenfield-Jane Heights (25) -79.515686 43.749149 125339 2015-12-31 21:00:00+00:00 2016-01-01 13:05:00+00:00 2015 21 16:05:00
129339 -79.506782 43.745850 125095 GO-20161149 2015-12-31T16:30:00.000Z 2016-01-01T03:35:00.000Z House 2135 210 Theft Of Motor Vehicle ... 25 Glenfield-Jane Heights (25) -79.506782 43.745850 125340 2015-12-31 16:30:00+00:00 2016-01-01 03:35:00+00:00 2015 16 11:05:00
129340 -79.515686 43.749149 125096 GO-20162866 2015-12-31T21:00:00.000Z 2016-01-01T13:05:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 25 Glenfield-Jane Heights (25) -79.515686 43.749149 125341 2015-12-31 21:00:00+00:00 2016-01-01 13:05:00+00:00 2015 21 16:05:00
129342 -79.557938 43.676212 125098 GO-2016611 2015-12-31T19:30:00.000Z 2016-01-01T08:11:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 7 Willowridge-Martingrove-Richview (7) -79.557938 43.676212 125343 2015-12-31 19:30:00+00:00 2016-01-01 08:11:00+00:00 2015 19 12:41:00
129343 -79.584366 43.639206 125099 GO-20163401 2015-12-31T19:30:00.000Z 2016-01-01T14:57:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 11 Eringate-Centennial-West Deane (11) -79.584366 43.639206 125344 2015-12-31 19:30:00+00:00 2016-01-01 14:57:00+00:00 2015 19 19:27:00

14867 rows × 34 columns

In [51]:
new2016= crime[crime['year']==2016]
new2016= new2016[new2016['Delay']>=datetime.timedelta(minutes=5)]
new2016
Out[51]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
58004 -79.419517 43.632015 68201 GO-20161521862 2016-08-27T22:00:00.000Z 2016-08-28T00:01:00.000Z Commercial 1430 100 Assault ... 82 Niagara (82) -79.419517 43.632015 68005 2016-08-27 22:00:00+00:00 2016-08-28 00:01:00+00:00 2016 22 02:01:00
58006 -79.208748 43.771454 68703 GO-20161743539 2016-10-01T03:30:00.000Z 2016-10-01T04:53:00.000Z Other 2120 200 B&E ... 135 Morningside (135) -79.208748 43.771454 68007 2016-10-01 03:30:00+00:00 2016-10-01 04:53:00+00:00 2016 3 01:23:00
58009 -79.293465 43.782120 68803 GO-20161727701 2016-09-28T19:30:00.000Z 2016-09-28T19:35:00.000Z Commercial 1430 100 Assault ... 118 Tam O'Shanter-Sullivan (118) -79.293465 43.782120 68010 2016-09-28 19:30:00+00:00 2016-09-28 19:35:00+00:00 2016 19 00:05:00
58011 -79.219978 43.796124 68704 GO-20161747864 2016-10-01T12:00:00.000Z 2016-10-01T21:38:00.000Z House 1450 100 Discharge Firearm - Recklessly ... 132 Malvern (132) -79.219978 43.796124 68012 2016-10-01 12:00:00+00:00 2016-10-01 21:38:00+00:00 2016 12 09:38:00
58014 -79.482071 43.726643 68804 GO-20161734493 2016-09-29T20:00:00.000Z 2016-09-29T22:21:00.000Z Commercial 1430 100 Assault ... 26 Downsview-Roding-CFB (26) -79.482071 43.726643 68015 2016-09-29 20:00:00+00:00 2016-09-29 22:21:00+00:00 2016 20 02:21:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
183552 -79.313149 43.686527 120782 GO-20162223165 2016-12-15T10:45:00.000Z 2016-12-15T21:12:00.000Z Other 1430 100 Assault ... 62 East End-Danforth (62) -79.313148 43.686527 120553 2016-12-15 10:45:00+00:00 2016-12-15 21:12:00+00:00 2016 10 10:27:00
183553 -79.379509 43.642414 120783 GO-20162223821 2016-12-15T21:42:00.000Z 2016-12-15T22:33:00.000Z Commercial 1460 100 Assault Peace Officer ... 77 Waterfront Communities-The Island (77) -79.379509 43.642414 120554 2016-12-15 21:42:00+00:00 2016-12-15 22:33:00+00:00 2016 21 00:51:00
183554 -79.337189 43.672913 120784 GO-20162222651 2016-12-15T15:10:00.000Z 2016-12-15T18:28:00.000Z Outside 1430 100 Assault ... 69 Blake-Jones (69) -79.337189 43.672913 120555 2016-12-15 15:10:00+00:00 2016-12-15 18:28:00+00:00 2016 15 03:18:00
183555 -79.189903 43.771389 120785 GO-20162312095 2016-12-31T04:00:00.000Z 2016-12-31T08:09:00.000Z Outside 1420 100 Assault With Weapon ... 136 West Hill (136) -79.189903 43.771389 120556 2016-12-31 04:00:00+00:00 2016-12-31 08:09:00+00:00 2016 4 04:09:00
183556 -79.189903 43.771389 120786 GO-20162312095 2016-12-31T04:00:00.000Z 2016-12-31T08:09:00.000Z Outside 1430 100 Assault ... 136 West Hill (136) -79.189903 43.771389 120557 2016-12-31 04:00:00+00:00 2016-12-31 08:09:00+00:00 2016 4 04:09:00

14559 rows × 34 columns

In [52]:
new2017= crime[crime['year']==2017]
new2017= new2017[new2017['Delay']>=datetime.timedelta(minutes=5)]
new2017
Out[52]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
58003 -79.517342 43.694614 68001 GO-20173055678 2017-11-23T16:20:00.000Z 2017-11-23T16:25:00.000Z Apartment 1450 120 Discharge Firearm With Intent ... 8 Humber Heights-Westmount (8) -79.517342 43.694614 68004 2017-11-23 16:20:00+00:00 2017-11-23 16:25:00+00:00 2017 16 00:05:00
58008 -79.517342 43.694614 68002 GO-20173055678 2017-11-23T16:20:00.000Z 2017-11-23T16:25:00.000Z Apartment 1450 120 Discharge Firearm With Intent ... 8 Humber Heights-Westmount (8) -79.517342 43.694614 68009 2017-11-23 16:20:00+00:00 2017-11-23 16:25:00+00:00 2017 16 00:05:00
58012 -79.252632 43.727394 68003 GO-20173059167 2017-11-23T20:00:00.000Z 2017-11-24T07:45:00.000Z Apartment 2120 200 B&E ... 124 Kennedy Park (124) -79.252632 43.727394 68013 2017-11-23 20:00:00+00:00 2017-11-24 07:45:00+00:00 2017 20 11:45:00
58013 -79.449455 43.650631 68602 GO-2017964649 2017-05-30T19:15:00.000Z 2017-05-31T19:07:00.000Z House 2120 200 B&E ... 86 Roncesvalles (86) -79.449455 43.650631 68014 2017-05-30 19:15:00+00:00 2017-05-31 19:07:00+00:00 2017 19 23:52:00
58016 -79.402389 43.636761 68501 GO-2017259009 2017-02-10T19:35:00.000Z 2017-02-10T19:42:00.000Z Apartment 1430 100 Assault ... 82 Niagara (82) -79.402389 43.636761 68017 2017-02-10 19:35:00+00:00 2017-02-10 19:42:00+00:00 2017 19 00:07:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
183996 -79.263550 43.725590 120597 GO-2017632674 2017-04-10T14:40:00.000Z 2017-04-10T14:46:00.000Z Outside 1450 120 Discharge Firearm With Intent ... 124 Kennedy Park (124) -79.263550 43.725590 120997 2017-04-10 14:40:00+00:00 2017-04-10 14:46:00+00:00 2017 14 00:06:00
183998 -79.224159 43.803452 120599 GO-2017632174 2017-04-10T12:10:00.000Z 2017-04-10T13:41:00.000Z Outside 1430 100 Assault ... 132 Malvern (132) -79.224159 43.803452 120999 2017-04-10 12:10:00+00:00 2017-04-10 13:41:00+00:00 2017 12 01:31:00
183999 -79.280182 43.693035 120600 GO-20171612094 2017-09-05T17:00:00.000Z 2017-09-06T08:01:00.000Z Commercial 2120 200 B&E ... 121 Oakridge (121) -79.280182 43.693035 121000 2017-09-05 17:00:00+00:00 2017-09-06 08:01:00+00:00 2017 17 15:01:00
201673 -79.341606 43.664845 196926 GO-20182309 2017-12-31T22:25:00.000Z 2018-01-01T09:27:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 70 South Riverdale (70) -79.341606 43.664845 196674 2017-12-31 22:25:00+00:00 2018-01-01 09:27:00+00:00 2017 22 11:02:00
201674 -79.278267 43.699040 196927 GO-20182005 2017-12-31T22:00:00.000Z 2018-01-01T07:13:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 121 Oakridge (121) -79.278267 43.699040 196675 2017-12-31 22:00:00+00:00 2018-01-01 07:13:00+00:00 2017 22 09:13:00

15130 rows × 34 columns

In [53]:
new2018= crime[crime['year']==2018]
new2018= new2018[new2018['Delay']>=datetime.timedelta(minutes=5)]
new2018
Out[53]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
126010 -79.338768 43.762894 131806 GO-20181312534 2018-07-18T07:30:00.000Z 2018-07-18T17:06:00.000Z Apartment 2120 200 B&E ... 45 Parkwoods-Donalda (45) -79.338768 43.762894 131011 2018-07-18 07:30:00+00:00 2018-07-18 17:06:00+00:00 2018 7 09:36:00
126011 -79.415077 43.644974 131807 GO-20181746606 2018-09-20T22:15:00.000Z 2018-09-20T22:20:00.000Z Apartment 1410 100 Aggravated Assault ... 82 Niagara (82) -79.415077 43.644974 131012 2018-09-20 22:15:00+00:00 2018-09-20 22:20:00+00:00 2018 22 00:05:00
126012 -79.438774 43.764461 131808 GO-20181745222 2018-09-20T13:00:00.000Z 2018-09-20T17:59:00.000Z House 2120 200 B&E ... 37 Willowdale West (37) -79.438774 43.764461 131013 2018-09-20 13:00:00+00:00 2018-09-20 17:59:00+00:00 2018 13 04:59:00
126013 -79.316711 43.774445 131809 GO-20181754235 2018-09-21T23:48:00.000Z 2018-09-22T00:26:00.000Z Outside 1420 100 Assault With Weapon ... 118 Tam O'Shanter-Sullivan (118) -79.316711 43.774445 131014 2018-09-21 23:48:00+00:00 2018-09-22 00:26:00+00:00 2018 23 00:38:00
126014 -79.316711 43.774445 131810 GO-20181754235 2018-09-21T23:48:00.000Z 2018-09-22T00:26:00.000Z Outside 1420 100 Assault With Weapon ... 118 Tam O'Shanter-Sullivan (118) -79.316711 43.774445 131015 2018-09-21 23:48:00+00:00 2018-09-22 00:26:00+00:00 2018 23 00:38:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
206373 -79.612595 43.720406 205839 GO-20182392312 2018-12-31T19:30:00.000Z 2018-12-31T19:44:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 1 West Humber-Clairville (1) -79.612595 43.720406 205939 2018-12-31 19:30:00+00:00 2018-12-31 19:44:00+00:00 2018 19 00:14:00
206374 -79.589706 43.742920 205840 GO-20182392034 2018-12-31T17:30:00.000Z 2018-12-31T18:39:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 2 Mount Olive-Silverstone-Jamestown (2) -79.589706 43.742920 205940 2018-12-31 17:30:00+00:00 2018-12-31 18:39:00+00:00 2018 17 01:09:00
206375 -79.412903 43.769276 205841 GO-20182389514 2018-12-31T06:50:00.000Z 2018-12-31T10:17:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 37 Willowdale West (37) -79.412903 43.769276 205941 2018-12-31 06:50:00+00:00 2018-12-31 10:17:00+00:00 2018 6 03:27:00
206376 -79.543449 43.770924 205842 GO-20182393328 2018-12-31T23:25:00.000Z 2018-12-31T23:33:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 21 Humber Summit (21) -79.543449 43.770924 205942 2018-12-31 23:25:00+00:00 2018-12-31 23:33:00+00:00 2018 23 00:08:00
206377 -79.375771 43.653171 205843 GO-2019692 2018-12-31T23:30:00.000Z 2019-01-01T02:11:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 75 Church-Yonge Corridor (75) -79.375771 43.653171 205943 2018-12-31 23:30:00+00:00 2019-01-01 02:11:00+00:00 2018 23 02:41:00

15976 rows × 34 columns

In [54]:
new2019= crime[crime['year']==2019]
new2019= new2019[new2019['Delay']>=datetime.timedelta(minutes=5)]
new2019
Out[54]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
126026 -79.373459 43.657307 131822 GO-201967688 2019-01-11T18:00:00.000Z 2019-01-11T22:32:00.000Z Outside 1457 100 Pointing A Firearm ... 73 Moss Park (73) -79.373459 43.657307 131027 2019-01-11 18:00:00+00:00 2019-01-11 22:32:00+00:00 2019 18 04:32:00
126027 -79.532402 43.594738 131823 GO-2019167546 2019-01-27T04:10:00.000Z 2019-01-27T10:23:00.000Z Commercial 2120 200 B&E ... 19 Long Branch (19) -79.532402 43.594738 131028 2019-01-27 04:10:00+00:00 2019-01-27 10:23:00+00:00 2019 4 06:13:00
126028 -79.384155 43.663906 131301 GO-2019374761 2019-02-28T13:05:00.000Z 2019-02-28T13:12:00.000Z Outside 1420 100 Assault With Weapon ... 75 Church-Yonge Corridor (75) -79.384155 43.663906 131029 2019-02-28 13:05:00+00:00 2019-02-28 13:12:00+00:00 2019 13 00:07:00
126046 -79.589424 43.732025 131838 GO-2019517104 2019-03-22T07:00:00.000Z 2019-03-22T07:13:00.000Z Other 2120 200 B&E ... 2 Mount Olive-Silverstone-Jamestown (2) -79.589424 43.732025 131047 2019-03-22 07:00:00+00:00 2019-03-22 07:13:00+00:00 2019 7 00:13:00
126047 -79.278397 43.705772 131839 GO-2019522924 2019-03-22T21:20:00.000Z 2019-03-22T22:44:00.000Z Apartment 2120 200 B&E ... 121 Oakridge (121) -79.278397 43.705772 131048 2019-03-22 21:20:00+00:00 2019-03-22 22:44:00+00:00 2019 21 01:24:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
206428 -79.593422 43.738930 205894 GO-2019333589 2019-02-21T23:45:00.000Z 2019-02-22T06:00:00.000Z House 2135 210 Theft Of Motor Vehicle ... 1 West Humber-Clairville (1) -79.593422 43.738930 205994 2019-02-21 23:45:00+00:00 2019-02-22 06:00:00+00:00 2019 23 06:15:00
206429 -79.416000 43.781639 205895 GO-2019329984 2019-02-21T07:30:00.000Z 2019-02-21T15:19:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 36 Newtonbrook West (36) -79.416000 43.781639 205995 2019-02-21 07:30:00+00:00 2019-02-21 15:19:00+00:00 2019 7 07:49:00
206430 -79.563538 43.682293 205896 GO-2019336030 2019-02-21T23:00:00.000Z 2019-02-22T13:27:00.000Z Outside 2135 210 Theft Of Motor Vehicle ... 7 Willowridge-Martingrove-Richview (7) -79.563538 43.682293 205996 2019-02-21 23:00:00+00:00 2019-02-22 13:27:00+00:00 2019 23 14:27:00
206432 -79.598869 43.743992 205898 GO-2019187512 2019-01-30T12:30:00.000Z 2019-01-30T12:47:00.000Z House 2135 210 Theft Of Motor Vehicle ... 1 West Humber-Clairville (1) -79.598869 43.743992 205998 2019-01-30 12:30:00+00:00 2019-01-30 12:47:00+00:00 2019 12 00:17:00
206434 -79.217125 43.814865 205900 GO-2019327189 2019-02-21T08:00:00.000Z 2019-02-21T08:09:00.000Z House 2135 210 Theft Of Motor Vehicle ... 132 Malvern (132) -79.217125 43.814865 206000 2019-02-21 08:00:00+00:00 2019-02-21 08:09:00+00:00 2019 8 00:09:00

17107 rows × 34 columns

In [55]:
n2014=new2014.event_unique_id.count()
n2015=new2015.event_unique_id.count()
n2016=new2016.event_unique_id.count()
n2017=new2017.event_unique_id.count()
n2018=new2018.event_unique_id.count()
n2019=new2019.event_unique_id.count()
In [56]:
mapp = folium.Map(location=SF_COORDINATES, tiles='Stamen Terrain', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "
 
state_geo = geopandas.read_file("data/Neighbourhoods.geojson")

crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))

crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']

folium.Choropleth(geo_data=state_geo,fill_opacity=-5,fill_color='red',
            line_weight=1).add_to(mapp)


folium.GeoJson(state_geo, name='geojson').add_to(mapp)


    
for index, each in new2014[0:n2014].iterrows():
    
  folium.Marker(
        location = [each['Lat'],each['Long']],
        icon=folium.Icon(icon='cloud'),
        clustered_marker = True).add_to(mapp)
      
for index, each in new[0:20].iterrows():
    
  folium.Circle(
        location = [each['LAT'],each['LONG']],colour="0A8A9F",radius=2500).add_to(mapp) 
 
mapp.save('mappolice1.html')
In [57]:
new_mci=['Assault']
new2019=new2019[new2019.MCI.isin(new_mci)]
In [58]:
new2019= new2019[new2019['Delay']<=datetime.timedelta(days=1)]
new2019= new2019[new2019['Delay']<=datetime.timedelta(hours=1)]
new2019
Out[58]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Hood_ID Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay
126028 -79.384155 43.663906 131301 GO-2019374761 2019-02-28T13:05:00.000Z 2019-02-28T13:12:00.000Z Outside 1420 100 Assault With Weapon ... 75 Church-Yonge Corridor (75) -79.384155 43.663906 131029 2019-02-28 13:05:00+00:00 2019-02-28 13:12:00+00:00 2019 13 00:07:00
126059 -79.384209 43.670807 131851 GO-2019933494 2019-05-22T13:13:00.000Z 2019-05-22T13:28:00.000Z Commercial 1480 100 Assault - Resist/ Prevent Seiz ... 98 Rosedale-Moore Park (98) -79.384209 43.670807 131060 2019-05-22 13:13:00+00:00 2019-05-22 13:28:00+00:00 2019 13 00:15:00
126060 -79.384209 43.670807 131852 GO-2019933494 2019-05-22T13:13:00.000Z 2019-05-22T13:28:00.000Z Commercial 1480 100 Assault - Resist/ Prevent Seiz ... 98 Rosedale-Moore Park (98) -79.384209 43.670807 131061 2019-05-22 13:13:00+00:00 2019-05-22 13:28:00+00:00 2019 13 00:15:00
126061 -79.384209 43.670807 131853 GO-2019933494 2019-05-22T13:13:00.000Z 2019-05-22T13:28:00.000Z Commercial 1480 100 Assault - Resist/ Prevent Seiz ... 98 Rosedale-Moore Park (98) -79.384209 43.670807 131062 2019-05-22 13:13:00+00:00 2019-05-22 13:28:00+00:00 2019 13 00:15:00
126062 -79.384209 43.670807 131854 GO-2019933494 2019-05-22T13:13:00.000Z 2019-05-22T13:28:00.000Z Commercial 1480 100 Assault - Resist/ Prevent Seiz ... 98 Rosedale-Moore Park (98) -79.384209 43.670807 131063 2019-05-22 13:13:00+00:00 2019-05-22 13:28:00+00:00 2019 13 00:15:00
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
201939 -79.516487 43.752510 196240 GO-20192497171 2019-12-28T01:08:00.000Z 2019-12-28T01:15:00.000Z Other 1430 110 Assault - Force/Thrt/Impede ... 25 Glenfield-Jane Heights (25) -79.516487 43.752510 196940 2019-12-28 01:08:00+00:00 2019-12-28 01:15:00+00:00 2019 1 00:07:00
201957 -79.437035 43.655216 196258 GO-20191843186 2019-09-24T20:45:00.000Z 2019-09-24T20:55:00.000Z House 1430 100 Assault ... 83 Dufferin Grove (83) -79.437035 43.655216 196958 2019-09-24 20:45:00+00:00 2019-09-24 20:55:00+00:00 2019 20 00:10:00
201965 -79.252602 43.727463 196266 GO-20192055135 2019-10-24T16:30:00.000Z 2019-10-24T16:39:00.000Z Apartment 1430 100 Assault ... 124 Kennedy Park (124) -79.252602 43.727463 196966 2019-10-24 16:30:00+00:00 2019-10-24 16:39:00+00:00 2019 16 00:09:00
201966 -79.252602 43.727463 196267 GO-20192055135 2019-10-24T16:30:00.000Z 2019-10-24T16:39:00.000Z Apartment 1420 100 Assault With Weapon ... 124 Kennedy Park (124) -79.252602 43.727463 196967 2019-10-24 16:30:00+00:00 2019-10-24 16:39:00+00:00 2019 16 00:09:00
201998 -79.232857 43.791912 196299 GO-20192477053 2019-12-24T12:05:00.000Z 2019-12-24T12:25:00.000Z Outside 1420 100 Assault With Weapon ... 132 Malvern (132) -79.232857 43.791912 196999 2019-12-24 12:05:00+00:00 2019-12-24 12:25:00+00:00 2019 12 00:20:00

4346 rows × 34 columns

In [63]:
mapp = folium.Map(location=SF_COORDINATES, tiles='openstreetmap', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "
 #popup_text = popup_text.format(new["FACILITY_NAME"],
 #                         new["ADDRESS"])
state_geo = geopandas.read_file("data/Neighbourhoods.geojson")

crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))

crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']
# add a marker for every record in the filtered data, use a clustered view
folium.Choropleth(geo_data=state_geo,fill_opacity=-5,fill_color='red',
            line_weight=1).add_to(mapp)


folium.GeoJson(state_geo, name='geojson').add_to(mapp)

def color(mci):
    if mci<=datetime.timedelta(minutes=15):
            col='green'
    elif mci<=datetime.timedelta(minutes=30):
        col='orange'
    else:
        col='red'
    return col

for index, each in new[0:20].iterrows():
    folium.Circle(location = [each['LAT'],each['LONG']],colour="red",radius=2500).add_to(mapp) 

for lat,lon,name,fac in zip(new['LAT'],new['LONG'],new['ADDRESS'],new['FACILITY_NAME']):
    folium.Marker(location=[lat,lon],popup=(folium.Popup(fac)),icon_color='red').add_to(mapp)

for lat,lon,name,delay in zip(new2019['Lat'],new2019['Long'],new2019['Neighbourhood'],new2019['Delay']):
    folium.Marker(location=[lat,lon],popup=(folium.Popup(name)),icon=folium.Icon(color=color(delay),icon_color='red')).add_to(mapp)
 
legend_html =   '''
                <div style="position: fixed; 
                            bottom: 100px; left: 100px; width: 280px; height: 160px; 
                            border:2px solid grey; z-index:9999; font-size:17px,color:green;
                            ">&nbsp; Legend <br>
                              &nbsp; less than 15 M &nbsp; <i class="fa fa-map-marker fa-2x" style="color:green"></i><br>
                              &nbsp; lesss than 30 M    &nbsp; <i class="fa fa-map-marker fa-2x" style="color:orange"></i><br>
                              &nbsp; greaterthan 30 m     &nbsp; <i class="fa fa-map-marker fa-2x" style="color:red"></i>
                </div>
                ''' 
mapp.get_root().html.add_child(folium.Element(legend_html))


mapp.save('policeradius.html')
In [ ]:

In [62]:
mapp = folium.Map(location=SF_COORDINATES, tiles='openstreetmap', zoom_start=12)
popup_text = "{}<br> Faclity name: {}<br> Address: "
 #popup_text = popup_text.format(new["FACILITY_NAME"],
 #                         new["ADDRESS"])
state_geo = geopandas.read_file("data/Neighbourhoods.geojson")

crimedata2 = pd.DataFrame(crimedata['Neighbourhood'].value_counts().astype(int))

crimedata2 = crimedata2.reset_index()
crimedata2.columns = ['Neighbour', 'Number']
# add a marker for every record in the filtered data, use a clustered view
folium.Choropleth(geo_data=state_geo,fill_opacity=-5,fill_color='red',
            line_weight=1).add_to(mapp)


folium.GeoJson(state_geo, name='geojson').add_to(mapp)

for index, each in new[0:20].iterrows():
    
  #popup_text = popup_text.format(each["FACILITY_NAME"],
   #                       each["ADDRESS"])
  folium.Circle(
        location = [each['LAT'],each['LONG']],colour="0A8A9F",radius=2500).add_to(mapp) 

for lat,lon,name,fac in zip(new['LAT'],new['LONG'],new['ADDRESS'],new['FACILITY_NAME']):
    folium.Marker(location=[lat,lon],popup=(folium.Popup(fac)),icon_color='green').add_to(mapp)


 
mapp.save('policeradius.html')
In [ ]:
state_geo 
In [ ]:
m = folium.Map([43, -100], zoom_start=4)

folium.Choropleth(
    geo_data=geopandas.read_file("data/Neighbourhoods.geojson"),data = crimedata2,
              columns = ['Neighbour', 'Number'],
    fill_color='BuPu',
    fill_opacity=0.3,
    line_weight=2,legend_name = 'Number of incidents per Neibourhood',
).add_to(m)

m.save('GeoJSON_and_choropleth_7.html')



           
In [64]:
# PLOTING ALTERNATIVE 
# COPied FROM THE PREVIOUS PLOTING SECTIONN

country = geopandas.read_file("data/Neighbourhoods.geojson")
country.head()
Out[64]:
_id AREA_ID AREA_ATTR_ID PARENT_AREA_ID AREA_SHORT_CODE AREA_LONG_CODE AREA_NAME AREA_DESC X Y LONGITUDE LATITUDE OBJECTID Shape__Area Shape__Length geometry
0 3501 25886861 25926662 49885 94 94 Wychwood (94) Wychwood (94) None None -79.425515 43.676919 16491505 3.217960e+06 7515.779658 None
1 3502 25886820 25926663 49885 100 100 Yonge-Eglinton (100) Yonge-Eglinton (100) None None -79.403590 43.704689 16491521 3.160334e+06 7872.021074 None
2 3503 25886834 25926664 49885 97 97 Yonge-St.Clair (97) Yonge-St.Clair (97) None None -79.397871 43.687859 16491537 2.222464e+06 8130.411276 None
3 3504 25886593 25926665 49885 27 27 York University Heights (27) York University Heights (27) None None -79.488883 43.765736 16491553 2.541821e+07 25632.335242 None
4 3505 25886688 25926666 49885 31 31 Yorkdale-Glen Park (31) Yorkdale-Glen Park (31) None None -79.457108 43.714672 16491569 1.156669e+07 13953.408098 None
In [65]:
florence = new2014
florence.head()
florence['coordinates'] = florence[['X', 'Y']].values.tolist()
florence.head()
Out[65]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay coordinates
307 -79.425995 43.642769 7082 GO-20143348754 2014-11-21T23:30:00.000Z 2014-11-22T17:36:00.000Z Commercial 1430 100 Assault ... Little Portugal (84) -79.425995 43.642769 7308 2014-11-21 23:30:00+00:00 2014-11-22 17:36:00+00:00 2014 23 18:06:00 [-79.42599490000002, 43.6427689]
466 -79.513802 43.719833 7414 GO-20141821905 2014-04-02T18:00:00.000Z 2014-04-03T16:47:00.000Z Apartment 2120 200 B&E ... Downsview-Roding-CFB (26) -79.513802 43.719833 7467 2014-04-02 18:00:00+00:00 2014-04-03 16:47:00+00:00 2014 18 22:47:00 [-79.51380159999998, 43.7198334]
559 -79.382378 43.671223 7923 GO-20141587326 2014-02-23T13:00:00.000Z 2014-02-24T07:31:00.000Z Other 2120 200 B&E ... Church-Yonge Corridor (75) -79.382378 43.671223 7560 2014-02-23 13:00:00+00:00 2014-02-24 07:31:00+00:00 2014 13 18:31:00 [-79.3823776, 43.6712227]
631 -79.304970 43.672829 7965 GO-20143345449 2014-11-21T23:30:00.000Z 2014-11-22T00:05:00.000Z House 1430 100 Assault ... The Beaches (63) -79.304970 43.672829 7632 2014-11-21 23:30:00+00:00 2014-11-22 00:05:00+00:00 2014 23 00:35:00 [-79.3049698, 43.672828700000004]
632 -79.391136 43.649898 7966 GO-20143344734 2014-11-21T11:46:00.000Z 2014-11-22T00:44:00.000Z Outside 1420 100 Assault With Weapon ... Kensington-Chinatown (78) -79.391136 43.649898 7633 2014-11-21 11:46:00+00:00 2014-11-22 00:44:00+00:00 2014 11 12:58:00 [-79.3911362, 43.6498985]

5 rows × 35 columns

In [66]:
florence['coordinates'] = florence['coordinates'].apply(Point)
florence = geopandas.GeoDataFrame(florence, geometry='coordinates')

florence.head()
Out[66]:
X Y Index_ event_unique_id occurrencedate reporteddate premisetype ucr_code ucr_ext offence ... Neighbourhood Long Lat ObjectId datetime datetime2 year hour Delay coordinates
307 -79.425995 43.642769 7082 GO-20143348754 2014-11-21T23:30:00.000Z 2014-11-22T17:36:00.000Z Commercial 1430 100 Assault ... Little Portugal (84) -79.425995 43.642769 7308 2014-11-21 23:30:00+00:00 2014-11-22 17:36:00+00:00 2014 23 18:06:00 POINT (-79.42599 43.64277)
466 -79.513802 43.719833 7414 GO-20141821905 2014-04-02T18:00:00.000Z 2014-04-03T16:47:00.000Z Apartment 2120 200 B&E ... Downsview-Roding-CFB (26) -79.513802 43.719833 7467 2014-04-02 18:00:00+00:00 2014-04-03 16:47:00+00:00 2014 18 22:47:00 POINT (-79.51380 43.71983)
559 -79.382378 43.671223 7923 GO-20141587326 2014-02-23T13:00:00.000Z 2014-02-24T07:31:00.000Z Other 2120 200 B&E ... Church-Yonge Corridor (75) -79.382378 43.671223 7560 2014-02-23 13:00:00+00:00 2014-02-24 07:31:00+00:00 2014 13 18:31:00 POINT (-79.38238 43.67122)
631 -79.304970 43.672829 7965 GO-20143345449 2014-11-21T23:30:00.000Z 2014-11-22T00:05:00.000Z House 1430 100 Assault ... The Beaches (63) -79.304970 43.672829 7632 2014-11-21 23:30:00+00:00 2014-11-22 00:05:00+00:00 2014 23 00:35:00 POINT (-79.30497 43.67283)
632 -79.391136 43.649898 7966 GO-20143344734 2014-11-21T11:46:00.000Z 2014-11-22T00:44:00.000Z Outside 1420 100 Assault With Weapon ... Kensington-Chinatown (78) -79.391136 43.649898 7633 2014-11-21 11:46:00+00:00 2014-11-22 00:44:00+00:00 2014 11 12:58:00 POINT (-79.39114 43.64990)

5 rows × 35 columns

In [78]:
florence.plot(figsize=(20,10));
In [79]:
# Plotting to see the hurricane overlay the US map:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')

# plotting the hurricane position on top with red color to stand out:
florence.plot(ax=base, color='Black', marker="*", markersize=100);
In [71]:
cam=new
cam
Out[71]:
_id PROVIDER ADDRESS FACILITY_NAME POSTAL_CODE LONG LAT coordinates
0 1 Toronto Police Services 2054 Davenport Road 11 Division M6N 1C8 -79.460825 43.671071 POINT (-79.46083 43.67107)
1 2 Toronto Police Services 200 Trethewey Drive 12 Division M6M 5E6 -79.486873 43.694571 POINT (-79.48687 43.69457)
2 3 Toronto Police Services 1435 Eglinton Avenue West 13 Division M6C 3Z4 -79.436680 43.698319 POINT (-79.43668 43.69832)
3 4 Toronto Police Services 350 Dovercourt Road 14 Division M6J 3E3 -79.425978 43.651297 POINT (-79.42598 43.65130)
4 5 Toronto Police Services 3699 Bloor Street West 22 Division M9A 1A2 -79.529181 43.643098 POINT (-79.52918 43.64310)
5 6 Toronto Police Services 5230 Finch Avenue West 23 Division M9V 0A1 -79.583518 43.743857 POINT (-79.58352 43.74386)
6 7 Toronto Police Services 40 Norfinch Drive 31 Division M3N 1X1 -79.527471 43.756742 POINT (-79.52747 43.75674)
7 8 Toronto Police Services 30 Ellerslie Avenue 32 Division M2N 1X8 -79.415084 43.771720 POINT (-79.41508 43.77172)
8 9 Toronto Police Services 50 Upjohn Road 33 Division M3B 2W1 -79.350065 43.751073 POINT (-79.35007 43.75107)
9 10 Toronto Police Services 2222 Eglinton Avenue East 41 Division M1K 2M2 -79.277107 43.730797 POINT (-79.27711 43.73080)
10 11 Toronto Police Services 242 Milner Avenue 42 Division M1S 5C4 -79.240009 43.789365 POINT (-79.24001 43.78937)
11 12 Toronto Police Services 4331 Lawrence Avenue East 43 Division M1E 2T4 -79.174053 43.770827 POINT (-79.17405 43.77083)
12 13 Toronto Police Services 51 Parliament Street 51 Division M5A 2Y5 -79.362139 43.651943 POINT (-79.36214 43.65194)
13 14 Toronto Police Services 255 Dundas Street West 52 Division M5T 2W5 -79.389714 43.654200 POINT (-79.38971 43.65420)
14 15 Toronto Police Services 75 Eglinton Avenue West 53 Division M4R 2G9 -79.400659 43.706051 POINT (-79.40066 43.70605)
15 16 Toronto Police Services 41 Cranfield Road 54 Division M4B 3H6 -79.309480 43.714600 POINT (-79.30948 43.71460)
16 17 Toronto Police Services 101 Coxwell Avenue 55 Division M4L 3B3 -79.317296 43.669171 POINT (-79.31730 43.66917)
17 23 Toronto Police Services 40 College Street Headquarters M5G 2J3 -79.385140 43.661349 POINT (-79.38514 43.66135)
In [72]:
cam['coordinates'] = cam[['LONG', 'LAT']].values.tolist()
cam.head()
Out[72]:
_id PROVIDER ADDRESS FACILITY_NAME POSTAL_CODE LONG LAT coordinates
0 1 Toronto Police Services 2054 Davenport Road 11 Division M6N 1C8 -79.460825 43.671071 [-79.46082507, 43.67107112]
1 2 Toronto Police Services 200 Trethewey Drive 12 Division M6M 5E6 -79.486873 43.694571 [-79.48687254, 43.69457056]
2 3 Toronto Police Services 1435 Eglinton Avenue West 13 Division M6C 3Z4 -79.436680 43.698319 [-79.43667976, 43.69831866]
3 4 Toronto Police Services 350 Dovercourt Road 14 Division M6J 3E3 -79.425978 43.651297 [-79.42597796, 43.65129691]
4 5 Toronto Police Services 3699 Bloor Street West 22 Division M9A 1A2 -79.529181 43.643098 [-79.52918128, 43.64309832]
In [73]:
cam['coordinates'] = cam['coordinates'].apply(Point)
cam.head()
cam = geopandas.GeoDataFrame(cam, geometry='coordinates')
cam.head()
Out[73]:
_id PROVIDER ADDRESS FACILITY_NAME POSTAL_CODE LONG LAT coordinates
0 1 Toronto Police Services 2054 Davenport Road 11 Division M6N 1C8 -79.460825 43.671071 POINT (-79.46083 43.67107)
1 2 Toronto Police Services 200 Trethewey Drive 12 Division M6M 5E6 -79.486873 43.694571 POINT (-79.48687 43.69457)
2 3 Toronto Police Services 1435 Eglinton Avenue West 13 Division M6C 3Z4 -79.436680 43.698319 POINT (-79.43668 43.69832)
3 4 Toronto Police Services 350 Dovercourt Road 14 Division M6J 3E3 -79.425978 43.651297 POINT (-79.42598 43.65130)
4 5 Toronto Police Services 3699 Bloor Street West 22 Division M9A 1A2 -79.529181 43.643098 POINT (-79.52918 43.64310)
In [74]:
# Plotting to see the hurricane overlay the US map:
fig, ax = plt.subplots(1, figsize=(130,200))
base = country.plot(ax=ax, color='Gray')

# plotting the hurricane position on top with red color to stand out:
florence.plot(ax=base, color='Black', marker="*", markersize=500);

# div around toronto
cam.plot(ax=base, color='Red', marker="o", markersize=5000);
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: